<?php
// +----------------------------------------------------------------------
// |店铺分类模型
// +----------------------------------------------------------------------
// |Data:2016-06-24 下午4:41
// +----------------------------------------------------------------------
// |Author: 吕志雄 <870923001@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Model;
use Common\Model\CommonModel;

class ShopCateModel extends CommonModel{
    protected $tableName = 'shop_cate';
    private $shop_cate_cache_list_key = 'sp_shop_cate_cache_list';
    private $shop_cate_cache_detail_key = 'sp_shop_cate_cache_detail_';

    /**
     * 获取列表
     * @return mixed
     */
    public function getList()
    {
        $list = S($this->shop_cate_cache_list_key);
        if(!$list){
            $list = $this->order('add_time desc')->select();
            S($this->shop_cate_cache_list_key,$list);
        }
        return $list;
    }

    /**
     * 获取详情
     * @param \Common\Model\编号ID $id
     * @return bool|mixed
     */
    public function detail($id){
        if(empty($id)) return false;
        $detail = S($this->shop_cate_cache_detail_key.$id);
        if(!$detail){
            $detail = $this->where('id='.$id)->find();
            S($this->shop_cate_cache_detail_key.$id,$detail);
        }
        return $detail;
    }

    public function getField($id,$filed='title')
    {
        if(empty($id)) return false;
        $detail = $this->detail($id);
        return $detail[$filed];
    }

    public function update($data, $id = null){
        $result = parent::update($data, $id);
        if($result){
            $this->clearCache($data['id']);
        }
        return $result;
    }

    public function del($id)
    {
        $result = parent::del($id);
        if($result){
            $this->clearCache($id);
        }
        return $result;
    }

    /**
     * 清理缓存
     * @param string $id
     */
    private function clearCache($id = ''){
        S($this->shop_cate_cache_list_key,null);
        !empty($id) && S($this->shop_cate_cache_detail_key.$id,null);
    }
}
?>